package com.study.listenter;

import com.alibaba.fastjson.JSON;
import com.study.bean.Student;
import com.study.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @Description TODO
 * @Author v_yangliu
 * @Date 2022/1/11 10:58
 * @Version 1.0
 **/
@Slf4j
public class EventListenter implements SpringApplicationRunListener {

    public EventListenter(SpringApplication app, String[] arg) {

    }

    @Override
    public void starting() {
        log.info("=====EventListenter启动了，{}=====", EventListenter.class.getClasses());
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        log.info("===========我在执行environmentPrepared方法{}========", environment.getProperty("app.name"));
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        log.info("=========我执行了contextPrepared{}方法===========", context);
        Map<String,Object> sysEnv= context.getEnvironment().getSystemEnvironment();
        Map<String,Object> sysProperties=context.getEnvironment().getSystemProperties();
        log.info("systemEnvironment:{}", JSON.toJSON(sysEnv));
        log.info("systemProperties:{}",JSON.toJSON(sysProperties));
       /*User user=context.getBean(User.class);
       log.info("user:{}",user.getName());*/
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        log.info("=========我执行了contextLoaded方法===========");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        log.info("=========我执行了started方法===========");
        User user = context.getBean(User.class);
        log.info("user.name:{}", user.getName());
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        log.info("=========我执行了running方法===========");
        /*Student stu = context.getBean(Student.class);
        log.info("student:{}",stu);*/
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        log.info("=========我执行了failed方法===========");
    }
}
